package com.jxl.application.controller.controller;

import com.google.common.base.Preconditions;
import com.jxl.application.controller.convert.AuthRolePermissionDtoConvert;
import com.jxl.dto.AuthRolePermissionDto;
import com.jxl.dto.Result;
import com.jxl.domain.entity.AuthRolePermissionBO;
import com.jxl.domain.service.AuthRolePermessionDomainService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色权限controller
 *
 * @author: ChickenWing
 * @date: 2023/11/2
 */
@RestController
@RequestMapping("/rolePermission/")
public class RolePermissionController {

    @Resource
    private AuthRolePermessionDomainService authRolePermessionDomainService;

    /**
     * 新增角色权限关联关系
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDto authRolePermissionDTO) {
        try {
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList()),"权限关联不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(),"角色不能为空!");
            AuthRolePermissionBO rolePermissionBO = AuthRolePermissionDtoConvert.INSTANCE.convertDtoToAuthRolePermissionBO(authRolePermissionDTO);
            return Result.ok(authRolePermessionDomainService.add(rolePermissionBO));
        } catch (Exception e) {
            return Result.fail("新增角色权限失败");
        }
    }

}
